/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.admin.rest;

import static com.automvc.common.service.CommService.hasNull;
import static com.automvc.common.service.CommService.isNullPara;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;
import org.teasoft.honey.osql.core.BeeFactoryHelper;

import com.aiteasoft.school.admin.entity.Assignclass;
import com.aiteasoft.school.admin.service.AssignclassService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-02-07 18:19:44
 */
@RestController
@RequestMapping("assignclass")
public class AssignclassRest {
	@Autowired
	ObjSQLService objSQLService;
	@Autowired
	ObjSQLRichService objSQLRichService;

	@Autowired
	AssignclassService assignclassService;

	@RequestMapping("/list")
	public Result list(Assignclass assignclass, 
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {

		Result result = new Result();
		if (isNullPara(result, assignclass)) return result;
		
		assignclass = assignclassService.process(assignclass);
		PageWarp pageWarp = assignclassService.countAndSelect(assignclass, (page - 1) * rows, rows);
		result.setTotal(pageWarp.getTotal());
		result.setRows(pageWarp.getList());

		return result;
	}
	
	@RequestMapping("/classno")
	public Result findAll() {

		Result result = new Result();
		Assignclass assignclass=new Assignclass();
		assignclass.setStatus("1");
		Condition condition=BeeFactoryHelper.getCondition();
		condition.selectDistinctField("classno");
		
		List<String[]> list=objSQLRichService.selectString(assignclass,condition);
		
		result.setTotal(list.size());
		result.setRows(list);

		return result;
	}
	
	@RequestMapping("/add")
	public Result insert(Assignclass assignclass) {

		Result result = new Result();
		if (isNullPara(result, assignclass)) return result;

		String checkFields = "classno,stuno,";
		if (hasNull(result, assignclass, checkFields)) return result;

		int num = objSQLService.insert(assignclass);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("insert failed!");
		return result;
	}

	@RequestMapping("/edit")
	public Result update(Assignclass assignclass) {

		Result result = new Result();
		if (isNullPara(result, assignclass)) return result;
		if(assignclassService.isWrongBizForUpdate(result, assignclass)) return result;

		String checkFields = "id,classno,stuno,";
		if (hasNull(result, assignclass, checkFields)) return result;
		
		assignclass = assignclassService.process(assignclass);
		
		int num = assignclassService.update(assignclass);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("update failed!");
		return result;
	}

}